// Project name:JavaRedis
// Creator:muhong
// Date time:2022/11/12,2:03 下午
// Name:JRedisClient

package org.gmh.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class JRedisClient {

    final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static int DEFAULT_PORT = 9090;

    private void connect() {
        String host = "127.0.0.1";
        Socket socket = null;
        try {
            socket = new Socket(host, DEFAULT_PORT);
            socket.setSoTimeout(5000);
            OutputStream outputStream = socket.getOutputStream();

            Scanner scanner = new Scanner(System.in);
            System.out.print("input:");
            String input = scanner.nextLine();
            while (!"exit".equals(input)) {
                outputStream.write((input + "\r\n").getBytes());

                InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String resp = bufferedReader.readLine();
                System.out.println("From remote:" + resp);
                System.out.print("input:");
                input = scanner.nextLine();
            }
            socket.setSoTimeout(1500);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class ReadData implements Runnable {

        private Socket socket;

        ReadData(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
        }
    }


    public static void main(String[] args) {
        JRedisClient client = new JRedisClient();
        client.connect();
    }
}
